package com.start.common.tools;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

/**
 * 分页工具
 */
public class CustomPageTools {

    public static <T> Page<T> page(int current, int size, List<T> list) {
        if (list == null) return new Page<>();
        CustomPage calculation = CustomPage.calculation(current, size, list.size());
        List<T> data = subList(list, calculation);
        return setPage(data, calculation);
    }

    public static <T> List<T> subList(List<T> list,CustomPage calculation){
        if(list==null)return null;
        if(list.size()<1)return list;
        return list.subList(calculation.getStartIndex(), calculation.getEndIndex());
    }

    public static <T> Page<T> setPage(List<T> list, CustomPage page) {
        Page<T> pageResult = new Page<>();
        pageResult.setTotal(page.getTotal());
        pageResult.setCurrent(page.getCurrent());
        pageResult.setSize(page.getSize());
        pageResult.setRecords(list);
        return pageResult;
    }
}
